package com.medic.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.medic.entity.group.pain.Alert;
import com.medic.entity.group.pain.Medic;
import com.medic.mapper.interf.MedicMapper;
import com.medic.service.interf.MedicServiceInterface;

@Service("medicService")
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) // 注明需要回滚
public class MedicServiceImpl implements MedicServiceInterface {

	@Autowired
	private MedicMapper medicMapper;

	/**
	 * 一起保存药品以及提醒
	 */
	@Override
	public boolean save(Medic object) throws Exception {
		boolean result = false;
		if (medicMapper.save(object) > 0) {
			result = true;
			if (object.getAlert() != null && object.getAlert().size() > 0) {
				if(medicMapper.saveAlerts(object.getAlert(), object.getMedicId()) <= 0){
					result = false;
				}
			}
		}
		return result;
	}

	@Override
	public boolean delete(Medic object) throws Exception {
		boolean result = false;
		if (medicMapper.delete(object) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public boolean update(Medic object) throws Exception {
		boolean result = false;
		if (medicMapper.update(object) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	public int saveAlerts(List<Alert> alerts, int medicId) throws Exception {
		return medicMapper.saveAlerts(alerts, medicId);
	}

	@Override
	public List<Medic> getAll() throws Exception {
		return medicMapper.selectAll();
	}

	@Override
	public List<Alert> getAlertByMedicId(int medicId) throws Exception {
		return medicMapper.selectAlertByMedicId(medicId);
	}

	@Override
	public int updateAlerts(List<Alert> alerts, int medicId) throws Exception {
		return medicMapper.updateAlerts(alerts, medicId);
	}

	@Override
	public boolean deleteAlert(int alertId, int medicId) throws Exception {
		boolean result = false;
		if (medicMapper.deleteAlert(alertId, medicId) > 0) {
			result = true;
		}
		return result;
	}

	@Override
	@Scheduled(cron = "0 5 0 * * ?")
	public void informSikerTakeMedic() throws Exception {

	}

}
